package com.example.attendancesystem.Handler;


import com.example.attendancesystem.Models.*;
import com.example.attendancesystem.Service.EmpService;
import com.example.attendancesystem.Service.PunchService;
import com.example.attendancesystem.Tools.CheckTimeTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class PunchController {

    @Autowired
    private PunchService punchService;

    @Autowired
    private EmpService empService;

    @RequestMapping("/punch")
    public String punch() {
        return "punch";
    }

    /**
     * 更新打卡时间
     *
     * @param morOnTime  上午上班打卡
     * @param morOffTime 上午下班
     * @param aftOnTime  下午上班
     * @param aftOffTime 下午下班
     * @return true 成功 false 失败
     */
    @RequestMapping("/updateCheckTime")
    @ResponseBody
    public Boolean updateCheckTime(String morOnTime, String morOffTime, String aftOnTime, String aftOffTime) {
        CheckTime checkTime = new CheckTime(morOnTime, morOffTime, aftOnTime, aftOffTime);
        return punchService.updateCheckTime(checkTime);
    }

    /**
     * 查询所有请假数据
     *
     * @return DataV2对象
     */
    @RequestMapping("/getAllLeaveRow")
    @ResponseBody
    public DataLeaveRow getAllLeaveRow() {
        DataLeaveRow dataLeaveRow;
        dataLeaveRow = punchService.getAllLeaveRow();
        return dataLeaveRow;
    }

    /**
     * 查询所有上午打卡记录
     *
     * @return DataV3对象
     */
    @RequestMapping("/getAllMorRow")
    @ResponseBody
    public DataMorRow getAllMorRow() {
        DataMorRow dataMorRow;
        dataMorRow = punchService.getAllMorRow();
        return dataMorRow;
    }

    /**
     * 查询所有下午打卡记录
     *
     * @return DataV4对象
     */
    @RequestMapping("/getAllAftRow")
    @ResponseBody
    public DataAftRow getAllAftRow() {
        DataAftRow dataAftRow;
        dataAftRow = punchService.getAllAftRow();
        return dataAftRow;
    }

    /**
     * 按日期查找 上午打卡记录
     *
     * @param queryDate 查找日期
     * @return DataV3对象
     */
    @RequestMapping("/getMorRowByDate")
    @ResponseBody
    public DataMorRow getMorRowByDate(String queryDate) {
        if (null == queryDate) {
            SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
            queryDate = date.format(new Date());
        }
        DataMorRow dataMorRow;
        dataMorRow = punchService.getMorRowByDate(queryDate);
        return dataMorRow;
    }

    /**
     * 按日期查找 下午打卡记录
     *
     * @param queryDate 查找日期
     * @return DataV4对象
     */
    @RequestMapping("/getAftRowByDate")
    @ResponseBody
    public DataAftRow getAftRowByDate(String queryDate) {
        if (null == queryDate) {
            SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
            queryDate = date.format(new Date());
        }
        DataAftRow dataAftRow;
        dataAftRow = punchService.getAftRowByDate(queryDate);
        return dataAftRow;
    }

    /**
     * 按日期查找 请假记录
     *
     * @param queryDate 查找日期
     * @return DataV4对象
     */
    @RequestMapping("/getLeaveRowByDate")
    @ResponseBody
    public DataLeaveRow getLeaveRowByDate(String queryDate) {
        if (null == queryDate) {
            SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
            queryDate = date.format(new Date());
        }
        DataLeaveRow dataLeaveRow;
        dataLeaveRow = punchService.getLeaveRowByDate(queryDate);
        return dataLeaveRow;
    }

    /**
     * 查询上午缺勤的学生
     *
     * @param queryDate 查询的日期
     * @return DataV0对象
     */
    @RequestMapping("/getMorAbsenceEmp")
    @ResponseBody
    public DataEmp getMorAbsenceEmp(String queryDate) {
        if (null == queryDate) {
            SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
            queryDate = date.format(new Date());
        }
        DataEmp dataEmp = new DataEmp();
        //先查询打卡时间 比较当前时间的 与 上午下班的时间的差值 如果差值 小于30 就是还没到 上午下班打卡时间还未结束
        //即无法查询缺勤记录 返回提示 还没到打卡时间
        CheckTime checkTime = empService.getCheckTime();
        int morOffVal = CheckTimeTools.getTimeDifference(checkTime.getMorOffTime());
        SimpleDateFormat date1 = new SimpleDateFormat("yyyyMMdd");
        String nowDate = date1.format(new Date());
        if (morOffVal <= 30 && (Integer.parseInt(queryDate) >= Integer.parseInt(nowDate))) {
            dataEmp.setCode(0);
            dataEmp.setMsg("还没到查询时间呢，请于打卡时间 " + checkTime.getMorOffTime() + " 半个小时之后查询");
        } else {
            dataEmp = punchService.getMorAbsenceEmp(queryDate);
        }
        return dataEmp;
    }

    /**
     * 查询下午缺勤的学生
     *
     * @param queryDate 查询的日期
     * @return DataV0对象
     */
    @RequestMapping("/getAftAbsenceEmp")
    @ResponseBody
    public DataEmp getAftAbsenceEmp(String queryDate) {
        if (null == queryDate) {
            SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
            queryDate = date.format(new Date());
        }
        DataEmp dataEmp = new DataEmp();
        CheckTime checkTime = empService.getCheckTime();
        int morOffVal = CheckTimeTools.getTimeDifference(checkTime.getAftOffTime());
        SimpleDateFormat date1 = new SimpleDateFormat("yyyyMMdd");
        String nowDate = date1.format(new Date());
        if (morOffVal <= 30 && (Integer.parseInt(queryDate) >= Integer.parseInt(nowDate))) {
            dataEmp.setCode(0);
            dataEmp.setMsg("还没到查询时间呢，请于打卡时间 " + checkTime.getAftOffTime() + " 半个小时之后查询");
        } else {
            dataEmp = punchService.getAftAbsenceEmp(queryDate);
            if (null == dataEmp.getData()) {
                dataEmp.setMsg("没有人缺勤");
            }
        }
        return dataEmp;
    }


}
